<?php
/*
function getImage($url,$filename='') {
    if(!$url) return false;
    if(!$filename) {
    $ext=strrchr(strtolower($url),'.') ;
    if($ext!='.gif' && $ext!='.jpg' && $ext!='.png') return false ;
    $str=explode('/',$url) ;
    $filename=$str[count($str)-1] ;
    //print $filename.�\n�;
    }

    ob_start();
    readfile($url);
    $img = ob_get_contents();
    ob_end_clean();
    $fp2=fopen($filename, 'a+') ;
    //file_put_contents($filename,$img);
    fwrite($fp2,$img);
    fclose($fp2) ;

    return $filename;
}
getImage('http://b47.photo.store.qq.com/http_imgload.cgi?/rurl4_b=35726cbf1e72b9432b4c3cb361bd1aa679d4ba56c5b3f700b030ebb1e5758dab9f9572a5a4a06642c2778d41a97a2c0ea666f07173f3b50134f7ad4260797329b9cfb5abdf683482dbf9eb07f5bf84c5d38eb847&a=35&b=47',"testpic.png");
*/
class Download{
	protected $fileType;
	protected $fileSize;
	protected $fileUrl;
	protected $savedDir = "downloads";
	protected $savedFileName;
	protected $errors;
	protected $message;
	protected $filetestflag =0;
	public $savedfilepath;
	
	public function __construct(){
		$this->fileType[] = "*";//default can download all files;
		$this->fileSize = "*";//not used for now ;
		
	}
	
	public function setFileType($type,$add=true){
		if (!$add){
			$this->fileType = "";
		}
		if (is_array($type)&&!empty($type)){
			foreach($type as $value){
				$this->fileType[] = $value;
			}
		}else {
			$this->fileType[] = $type;
		}
	}
	
	public function setFileSize($size){
		$this->size = (float)$size;
	}
	
	public function setFileUrl($url){
		$this->fileUrl = $url;
	}
	
	public function getDefaultFileName(){
		if (strrchr($this->fileUrl,"/")){
			$url = explode("/",$this->fileUrl);
		}else {
			$url = explode("\\",$this->fileUrl);
		}
		$len = count($url)-1;
		$name = $url[$len];
		
		return $name;
	}
	
	public function getFileTypes(){
		return $this->fileType;
	}
	
	public function getFileSize(){
		return $this->fileSize;
	}
	public function setError($error){
		$this->errors[] = $error;
	}
	public function getErrors(){
		return $this->errors;
	}
	
	public function getMessage(){
		return $this->message;
	}
	
	
	public function filetest($filepath){
		if(file_exists($filepath)){
			
			$this->filetestflag +=1;
			$filearr = explode(".",$filepath);
			$count = count($filearr);
			$last = $count-1;
			for ($i=0;$i<$last;$i++){
				$temarr[] = $filearr[$i];
			}
			$fileprex = implode("/",$temarr);
			
			if (strrchr($fileprex,"(")){
				$fileprexarr = explode("(",$fileprex);
				$fileprex = $fileprexarr[0];
			}
			$filepath = $fileprex."(".$this->filetestflag.")".".".$filearr[$last];
			
			$this->filetest($filepath);
		}else {
			//echo $filepath." last <br />";
			$this->savedfilepath = $filepath;
			return $filepath;
		}
	}
	
	/**
	 * Enter description here...
	 *
	 * @param string $fileUrl;
	 * @param string $filename saved file name;
	 * @return unknown
	 */
	public function doDownloadFiles($fileUrl,$filename=""){
		$this->setFileUrl($fileUrl);
		$type = substr(strrchr($fileUrl,"."),1);
		if (!in_array($type,$this->fileType)&&!in_array("*",$this->fileType)){
			$this->errors[] = "file type not allowed.";
			//$this->message["error"] .= $this->errors[];
			return $this;
		}
		$directory = $this->savedDir;
		if (!is_dir($directory)){
					mkdir($directory);
		}
		if(!$filename){
			$filename = $directory."/".$this->getDefaultFileName();
		}else{
			if (strrchr($filename,"/")){
				$lasttemp ="";
				$dirs = explode("/",$filename);
				
				
				$dircount = count($dirs);
				
				if (strrchr($filename,"/")=="/"){
					$lasttemp = $this->getDefaultFileName();
					
				}else{
					$dircount = $dircount-1;
					$lasttemp = "/".$dirs[$dircount];
				}
		
				for($i=0;$i<$dircount;$i++){
					$dir = $dirs[$i];
					if (!is_dir($directory."/".$dir)){
						mkdir($directory."/".$dir);	
					}
					$directory.= "/".$dir;
					
				}
				$filename = $directory.$lasttemp;
			}
		}
		
		$filename = $this->filetest($filename);
		
		if (!$filename){
			$filename = $this->savedfilepath;
			
		}
		
		if(strlen(strrchr($filename,"."))>5||!strrchr($filename,".")){
		
			$filename .=".jpg";
		}
		
		ob_start();
	    readfile($fileUrl);
	    $img = ob_get_contents();
	    ob_end_clean();
	    $fp = fopen($filename, 'a') ;
	    if ($fp){
	    	fwrite($fp,$img);
	    	fclose($fp);
	    	$this->message["log"] = "success download file from $fileUrl;<br />saved at : $filename";
	    	$this->message["url"] = $filename;
	    }else{
	    	$this->errors[] = "failed open file ".$filename;
	    }
	    //file_put_contents($filename,$img);
	    $this->message["errors"] = $this->errors;
	    return $this;
	}
	
}
?>